//    Copyright 2013 Serge Kosachiov.
//    This file is part of bulkmail-jsf.
//
//    BulkMail is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    BulkMail is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with BulkMail.  If not, see <http://www.gnu.org/licenses/>.


package bulkmail;

import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.servlet.http.HttpSession;

public class LoginBean {

	protected Logger logger = Logger.getLogger(this.getClass().getName());
	
	public static final String AUTH_KEY = "app.user.name";
	
	private String login = "";
	private String password;
	private boolean auth = false;
	
	//
	public String getLogin() {
		return login;
	}

	public void setLogin(String name) {
		this.login = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	////
	
	public boolean getAuth() {
		return auth;
	}

	public String login() {
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(AUTH_KEY, login);
		logger.info("login: " + login);
		auth = true;
		return "secret";
	}
	
	public String logout() {
		MailBean mailBean = (MailBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("mailBean");
		mailBean.removeAttachment();
		//
	    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(AUTH_KEY);
	    HttpSession httpSession = (HttpSession)
	    		FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	    httpSession.invalidate();
	    logger.info("logout: " + login);
	    auth = false;
	    return "/login";
	}

	////
	public String submit () {
		try {
			Properties prop = new Properties();
			prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
			// Set up the environment for creating the initial context
			Hashtable<String, String> env = new Hashtable<String, String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
			env.put(Context.PROVIDER_URL, prop.getProperty("PROVIDER_URL"));
			env.put(Context.SECURITY_AUTHENTICATION, prop.getProperty("SECURITY_AUTHENTICATION"));
			env.put(Context.SECURITY_PRINCIPAL, "uid=" + login + "," + prop.getProperty("SECURITY_BASE"));
			env.put(Context.SECURITY_CREDENTIALS, password);
			@SuppressWarnings("unused")
			DirContext ctx = new InitialDirContext(env);
			login();
		} catch (NamingException ex) {
			ex.printStackTrace();
			logout();
		} catch (Exception ex) {
			ex.printStackTrace();
			logout();
		}
		if (auth) return "/user/sendmail";
		else return "/authfailed";
	}

}
